package com.sangeng.handler.security;

import com.alibaba.fastjson.JSON;
import com.sangeng.domain.ResponseResult;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.utils.WebUtils;
import org.apache.catalina.connector.Response;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName: AuthenticationEntryPointImpl
 * Package: com.sangeng.handler.security
 * Description:
 *
 * @Author hjh
 * @Create 2024/8/15 21:26
 * @Version 1.0
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException authenticationException) throws IOException, ServletException {
       // authenticationException.printStackTrace();

        ResponseResult result=null;
        if (authenticationException instanceof BadCredentialsException){
            result=
            ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR.getCode(),authenticationException.getMessage());
        } else if (authenticationException instanceof InsufficientAuthenticationException) {
            result=
            ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }else {
            result=
            ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),"认证授权失败");
        }

        //响应给前端
        WebUtils.renderString(httpServletResponse, JSON.toJSONString(result));

    }
}
